ActiveAdmin.register Order do
  permit_params :admin_user_id, :order_no, :good_name, :good_amount, :good_price, :pay_type, :total, :pay_amount

  actions :index, :new, :create, :show

  filter :order_no
  filter :good_name

  scope "全部订单", default: true do |obj|
    obj.where(admin_user_id: current_admin_user.id)
  end

  index do
    column :order_no
    column :good_name
    column :good_price
    column :good_amount
    column :total
    column :pay_type
    column :pay_amount
    column :status do |order|
      case order.status
        when 0
          '未付款'
        when 1
          '已支付'
        else
          '未知状态'
      end
    end
    actions
  end

  show do
    attributes_table do
      row :order_no
      row :good_name
      row :good_amount
      row :total
      row :pay_type
      row "二维码" do |order|
        raw RQRCode::QRCode.new(order.code_url, :size => 6, :level => :h).as_svg(offset: 0, color: '000',
                                                                                 shape_rendering: 'crispEdges',
                                                                                 module_size: 6)
      end
      row ' ' do |order|
        link_to('支付完成', admin_orders_path, class: :button)
      end
    end
  end

  form do |f|
    f.inputs '' do
      f.input :admin_user_id, as: :hidden, input_html: {value: current_admin_user.id}
      f.input :order_no, as: :hidden, input_html: {value: Time.now.strftime('%Y%m%d%H%M%S') + %W'0 1 2 3 4 5 6 7 8 9'.sample(3).join}
      f.input :good_name
      f.input :good_price
      f.input :good_amount
      f.input :pay_type, as: :select, collection: [['微信']], include_blank: false
      f.input :total
    end
    f.submit
  end
end
